package com.longai.controller;

import java.io.IOException;
import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import util.Page;

import com.alibaba.fastjson.JSON;
import com.google.gson.Gson;
import com.longai.model.Event;
import com.longai.model.News;
import com.longai.model.Picture;
import com.longai.model.UploadFileInfo;
import com.longai.model.User;
import com.longai.service.EventService;
import com.longai.service.FileUpload;
import com.longai.vo.EventVo;

import functions.Px;

@Controller
@RequestMapping("event")
public class EventAction {
    @Autowired
	EventService eventService;
    FileUpload fileupload;
	@RequestMapping(value = "getEvent")//接口根据位置返回活动
	@ResponseBody
	public Object getEvent(@RequestParam(value = "starttime") String starttime,
			@RequestParam(value = "location") String location,
			@RequestParam("currentPage") String currentPage,
			@RequestParam("amount") String amount) throws ParseException{
		Page u=eventService.findByTime(location,starttime,currentPage,amount);
		List<Event> list = u.getList();
		if(!list.isEmpty()){
//			List<Event> list2 = new ArrayList<Event>();
			List<EventVo> list3 = new ArrayList<EventVo>();
//			for (Event img : list) {
//				img.setCover(Px.px("eventImage"));
//				list2.add(img);
//			}
			for(Event s:list){
				EventVo q=new EventVo();
				q.setContenturl(s.getContenturl());
				q.setCover(s.getCover());
				q.setEndtime(DateToLong(s.getEndtime()));
				q.setEventId(s.getEventId());
				q.setLocation(s.getLocation());
				q.setPermission(s.getPermission());
				q.setSponsors(s.getSponsors());
				q.setStarttime(DateToLong(s.getStarttime()));
				q.setStatus(s.getStatus());
				q.setTitle(s.getTitle());
				list3.add(q);
			}
			u.setList(list3);
			return u;
		}
		else 
//		User gg=new User();
//		return gg;
			return "";
	}
	@RequestMapping("eventManager")//跳转到活动管理列表
	public String eventManager(Model model){
		List<Event> u=eventService.findAll();
		model.addAttribute("events", u);
		return "eventManager";
	}
	@RequestMapping("addEvent")
	public String addEvent(String eventId,Model model){
		if(eventId==null)
			return "addEvent";
		else{
		int num=Integer.parseInt(eventId);
		Event event=eventService.findById(num);
		model.addAttribute("event", event);
		return "addEvent";
		}
	}
	@RequestMapping("addEventEdit")//修改或者新增活动
	public String addEventEdit(Event event,@RequestParam("image")MultipartFile cover,HttpServletRequest request){
		try {
			UploadFileInfo uploadFileInfo =  fileupload.upload(cover, "eventImage", request);
			if(uploadFileInfo.isStatus()){	
				event.setCover(uploadFileInfo.getName());
				//System.out.println(uploadFileInfo.getName());
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		eventService.save(event);
		return "redirect:eventManager";
	}
	@RequestMapping("deleteEvent")//根据id删除用户
	public String deleteEvent(Event event){
		eventService.delete(event);
		return "redirect:eventManager";
	}
	
	@RequestMapping("load")//后台管理员进入登录界面
	public String load() {  
        return   "login";
    }  
	@RequestMapping(value = "loginManager",method = RequestMethod.POST)//登录验证
    public String loginManager(@RequestParam(value = "username") String name,
                            @RequestParam(value = "password") String password){
			if(name.equals("admin")&&password.equals("admin")){
				
			return "redirect:eventManager";
		}
		else {
				return "login";
			}
    }
	public String longToDate(long time) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String u=sdf.format(new Date(time));
		return u;
	}
	public long DateToLong(String time) throws ParseException{   
		long utime;
		SimpleDateFormat formatter=new SimpleDateFormat("yyyy-MM-dd");
	    utime=formatter.parse(time).getTime();
		return utime;
	}
}
